//
//  NSString+WNTimer.m
//  iOSOCProjectDirectory
//
//  Created by BHmac on 2017/6/9.
//  Copyright © 2017年 XieXuan. All rights reserved.
//

#import "NSString+WNTimer.h"

@implementation NSString (WNTimer)

//获取当前时间
+ (NSString *)getCurrentTimeWithType:(NSString *)type{
    NSDate *  senddate=[NSDate date];
    
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    
    
    [dateformatter setDateFormat:type];
    
    NSString *  locationString=[dateformatter stringFromDate:senddate];
    
    return locationString;
}
+(NSString * )featureWeekdayWithDate:(NSInteger)num  withWeekType:(WeekType)type{
    //    NSDate *  senddate=[NSDate date];
    
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    
    [dateformatter setDateFormat:@"yyyy-MM-dd"];
    
    //    NSString *  locationString=[dateformatter stringFromDate:senddate];
    NSInteger week =[self getNowWeekday] + num;
    if (week>7) {
        week = week%7;
    }
    
    NSLog(@"week --->>>%ld",(long)week);
    if (type == WeekTypeLetter) {
        switch (week) {
            case 1:
                return @"日";
                break;
            case 2:
                return @"一";
                break;
            case 3:
                return @"二";
                break;
            case 4:
                return @"三";
                break;
            case 5:
                return @"四";
                break;
            case 6:
                return @"五";
                break;
            case 7:
                return @"六";
                break;
            default:
                break;
        }
    }else{
        switch (week) {
            case 1:
                return @"星期日";
                break;
            case 2:
                return @"星期一";
                break;
            case 3:
                return @"星期二";
                break;
            case 4:
                return @"星期三";
                break;
            case 5:
                return @"星期四";
                break;
            case 6:
                return @"星期五";
                break;
            case 7:
                return @"星期六";
                break;
            default:
                break;
        }
        
    }
    
    return nil;
}
//获取当前是星期几
+(NSInteger)getNowWeekday{
    
    NSCalendar * calendar =[[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents * comps =[[NSDateComponents alloc]init];
    NSInteger unitFlags = NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitWeekday|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond;
    NSDate * now =[NSDate date];
    calendar.locale =[[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"];
    comps = [calendar components:unitFlags fromDate:now];
    return [comps weekday];
    
}
//获取前多少天的日期

+ (NSString *)getWeekTime:(int)dayNum
{
    
    //    NSDate *nowDate = [NSString dateFromString:startStr];
    NSDate *  nowDate=[NSDate date];
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *comp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitDay fromDate:nowDate];
    
    // 获取今天是几号
    NSInteger day = [comp day];
    
    // 在当前日期(去掉时分秒)基础上加上差的天数
    NSDateComponents *firstDayComp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay  fromDate:nowDate];
    [firstDayComp setDay:day+dayNum];
    NSDate *firstDayOfWeek = [calendar dateFromComponents:firstDayComp];
    
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyyMMdd"];
    
    NSString *firstDay = [formatter stringFromDate:firstDayOfWeek];
    return firstDay;
    
}
//判断某个日期是星期几
//str 格式 yyyyMMdd
+(NSString *)getWeekDayTime:(NSString *)str{
    
    NSDateComponents *_comps = [[NSDateComponents alloc] init];
    
    NSString * day =[str substringWithRange:NSMakeRange(6, 2)];
    NSString * month =[str substringWithRange:NSMakeRange(4, 2)];
    NSString * year =[str substringWithRange:NSMakeRange(0, 4)];
    DLog(@"day-->>%@,month---->>%@,year --->>>%@",day,month,year);
    
    [_comps setDay:[day intValue]];
    [_comps setMonth:[month intValue]];
    [_comps setYear:[year intValue]];
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDate *_date = [gregorian dateFromComponents:_comps];
    NSDateComponents *weekdayComponents =
    [gregorian components:NSCalendarUnitWeekday fromDate:_date];
    int _weekday = (int)[weekdayComponents weekday];
    NSLog(@"_weekday::%d",_weekday);
    
    switch (_weekday) {
        case 1:
            return @"星期日";
            break;
        case 2:
            return @"星期一";
            break;
        case 3:
            return @"星期二";
            break;
        case 4:
            return @"星期三";
            break;
        case 5:
            return @"星期四";
            break;
        case 6:
            return @"星期五";
            break;
        case 7:
            return @"星期六";
            break;
        default:
            break;
    }
    return nil;
    
}

@end
